package com.android.example.myapp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener{
    private SharedPreferences mShared; // 声明一个共享参数对象
    private Button btnRegister;
    private EditText etAccount,etPass,etPassConfirm;//账户 密码
    private CheckBox cbAgree;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        etAccount = findViewById(R.id.et_account);//账户
        etPass = findViewById(R.id.et_password);//密码
        etPassConfirm = findViewById(R.id.et_password_confirm);//确认密码
        cbAgree = findViewById(R.id.cb_agree);//勾选项
        //btnRegister = findViewById(R.id.btn_register);//按钮
        //findViewById(R.id.btn_save).setOnClickListener(this);//保存到共享参数按钮

        findViewById(R.id.btn_register).setOnClickListener(this);//保存到共享参数按钮

        // 从share.xml中获取共享参数对象
        mShared = getSharedPreferences("share", MODE_PRIVATE);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_register) {//按钮id
            String name = etAccount.getText().toString();//账户
            String etpass = etPass.getText().toString();//密码
            String pass = etPassConfirm.getText().toString();//确认密码
            if (TextUtils.isEmpty(name)) {
                showToast("请先填写姓名");
                return;
            } else if (TextUtils.isEmpty(etpass)) {
                showToast("请先填写密码");
                return;
            } else if (TextUtils.isEmpty(pass)) {
                showToast("请先确认密码");
                return;
            }
            //判断密码是否一致，一致统一存入共享
          //判断用户名
            if(TextUtils.isEmpty(name)){
                Toast.makeText(this,"用户名不能为空",Toast.LENGTH_LONG).show();
                return;
            }

            //判断密码
            if(TextUtils.isEmpty(etpass)){
                Toast.makeText(this,"密码不能为空",Toast.LENGTH_LONG).show();
                return;
            }
            if(!TextUtils.equals(etpass,pass)){
                Toast.makeText(this,"密码不一致",Toast.LENGTH_LONG).show();
                return;
            }
            if(!cbAgree.isChecked()){
                Toast.makeText(this,"请同意用户协议",Toast.LENGTH_LONG).show();
            }
            SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
            editor.putString("name", name); // 添加一个名叫name的字符串参数
            editor.putString("pass", pass); // 添加一个名叫name的字符串参数
            editor.commit(); // 提交编辑器中的修改
            showToast("数据已写入共享参数");
            //跳转到登入页面
            startActivity(new Intent(this, MainActivity.class));
/*
            editor.putBoolean("married", bMarried); // 添加一个名叫married的布尔型参数
            editor.putString("update_time", DateUtil.getNowDateTime("yyyy-MM-dd HH:mm:ss"));*/

        }
    }

    private void showToast(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }
}